package com.danger.dy.blogger.chatgpt;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.CountDownLatch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

/**
 * MyStreamingResponseBody
 */
@Slf4j
public class MyStreamingResponseBody implements StreamingResponseBody {
    CountDownLatch countDownLatch;
    public MyStreamingResponseBody(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void writeTo(OutputStream outputStream) throws IOException {
        // 将数据写入OutputStream
        // 这里你可以根据需要从文件、数据库等读取数据，并将数据写入到 outputStream 中
        outputStream.write("Hello".getBytes());
        log.info("wwwwwwwwwwww");
        try {
            Thread.sleep(1000); // 模拟耗时操作
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        outputStream.write("World".getBytes());
        outputStream.flush();
        countDownLatch.countDown();
    }
}
